home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / haziran / 19 / setup.exe / data.z / iop480_diag.c < prev    next >
C/C++ Source or Header  |  2001-04-11  |  17KB  |  472 lines

  1. ////////////////////////////////////////////////////////////////
  2. // File - IOP480_DIAG.C
  3. //
  4. // o A simple diagnostics program that lets you access the
  5. //   PLX IOP 480 registers and local memory. 
  6. // o This program is meant to be used as an example for using the IOP480_LIB.H API,
  7. //   you may use it as a skeleton for your driver, or 'cut & paste' parts
  8. //   of it into your device driver code.
  9. // o For a more advanced monitor program, use the standard PLXMON.EXE
  10. //   from PLX.
  11. // 
  12. ////////////////////////////////////////////////////////////////
  13.  
  14. #include "../lib/iop480_lib.h"
  15. #include "../../../samples/shared/pci_diag_lib.h"
  16. #include <stdio.h>
  17.  
  18. // input of command from user
  19. static char line[256];
  20.  
  21. void PLX_EditReg(IOP480_HANDLE hPlx)
  22. {
  23.     struct 
  24.     {
  25.         CHAR *name;
  26.         DWORD dwOffset;
  27.         DWORD dwVal;
  28.     } fields[30];
  29.  
  30.     int cmd;
  31.     int i;
  32.     int field_count;
  33.  
  34.     i = 0;
  35.     fields[i].name = "DEVINIT"; fields[i++].dwOffset = 0x80;
  36.     fields[i].name = "LAS0RR"; fields[i++].dwOffset = 0xa0;
  37.     fields[i].name = "LAS0BA"; fields[i++].dwOffset = 0xa4;
  38.     fields[i].name = "LAS1RR"; fields[i++].dwOffset = 0xa8;
  39.     fields[i].name = "LAS1BA"; fields[i++].dwOffset = 0xac;
  40.     fields[i].name = "EROMRR"; fields[i++].dwOffset = 0xc0;
  41.     fields[i].name = "EROMBA"; fields[i++].dwOffset = 0xc4;
  42.     fields[i].name = "MBOX0"; fields[i++].dwOffset = 0x180;
  43.     fields[i].name = "MBOX1"; fields[i++].dwOffset = 0x184;
  44.     fields[i].name = "MBOX2"; fields[i++].dwOffset = 0x188;
  45.     fields[i].name = "MBOX3"; fields[i++].dwOffset = 0x18c;
  46.     fields[i].name = "MBOX4"; fields[i++].dwOffset = 0x190;
  47.     fields[i].name = "MBOX5"; fields[i++].dwOffset = 0x194;
  48.     fields[i].name = "MBOX6"; fields[i++].dwOffset = 0x198;
  49.     fields[i].name = "MBOX7"; fields[i++].dwOffset = 0x19c;
  50.     fields[i].name = "P2LDBELL"; fields[i++].dwOffset = 0x1a0;
  51.     fields[i].name = "L2PDBELL"; fields[i++].dwOffset = 0x1a4;
  52.     fields[i].name = "PINTSTAT"; fields[i++].dwOffset = 0x1b0;
  53.     fields[i].name = "PINTENB"; fields[i++].dwOffset = 0x1b4;
  54.     fields[i].name = "LINTSTAT"; fields[i++].dwOffset = 0x1b8;
  55.     fields[i].name = "LINTENB"; fields[i++].dwOffset = 0x1bc;
  56.     field_count = i;
  57.     do
  58.     {
  59.         int row;
  60.         int col;
  61.         int row_count = field_count/2 + field_count%2;
  62.  
  63.         printf ("\n");
  64.         printf ("Edit PLX IOP 480 registers\n");
  65.         printf ("--------------------------------\n");
  66.         for (row = 0; row<row_count; row++)
  67.         {
  68.             for (col = 0; col<=1; col++)
  69.             {
  70.                 if (col==0) i = row;
  71.                 else i = row + row_count;
  72.  
  73.                 if (i<field_count)
  74.                 {
  75.                     char buf[10];
  76.                     fields[i].dwVal = IOP480_ReadReg(hPlx, fields[i].dwOffset);
  77.                     sprintf(buf, "%08x",fields[i].dwVal);
  78.                     printf ("%2d. %8s : %s     ",i+1, fields[i].name, buf);
  79.                 }
  80.                 if (col==1) printf ("\n");
  81.             }
  82.         }
  83.  
  84.         printf ("99. Back to main menu\n");
  85.         printf ("Choose register to write to, or 99 to exit: ");
  86.         cmd = 0;
  87.         fgets(line, sizeof(line), stdin);
  88.         sscanf (line, "%d",&cmd);
  89.         if (cmd>=1 && cmd <=21)
  90.         {
  91.             i = cmd-1;
  92.             printf ("Enter value to write to %s register (or 'X' to cancel): ",fields[i].name);
  93.             fgets(line, sizeof(line), stdin);
  94.             if (toupper (line[0])!='X')
  95.             {
  96.                 DWORD dwVal;
  97.                 dwVal = 0;
  98.                 sscanf (line,"%x",&dwVal);
  99.                 IOP480_WriteReg(hPlx, fields[i].dwOffset, dwVal);
  100.             }
  101.         }
  102.     } while (cmd!=99);
  103. }
  104.  
  105. char *PLX_GetAddrRangeName(IOP480_ADDR addrSpace)
  106. {
  107.     return 
  108.         addrSpace==IOP480_ADDR_SPACE0 ? "Addr Space 0 - (BAR1)" :
  109.         addrSpace==IOP480_ADDR_SPACE1 ? "Addr Space 1 - (BAR2)" :
  110.         addrSpace==IOP480_ADDR_SPACE2 ? "Addr Space 2 - (BAR3)" :
  111.         addrSpace==IOP480_ADDR_SPACE3 ? "Addr Space 3 - (BAR4)" :
  112.         addrSpace==IOP480_ADDR_SPACE4 ? "Addr Space 4 - (BAR5)" :
  113.         addrSpace==IOP480_ADDR_EPROM ? "EEPROM Addr Space" : "Invalid";
  114. }
  115.  
  116. void PLX_BoardAccess(IOP480_HANDLE hPlx, BOOL fLocalAddr)
  117. {
  118.     int cmd, cmd2;
  119.     DWORD addr, data, i;
  120.     IOP480_ADDR ad_sp = IOP480_ADDR_SPACE0;
  121.     IOP480_MODE ad_mode = IOP480_MODE_DWORD;
  122.  
  123.     char *pcMemoryType = fLocalAddr ? "local address" : "offset";
  124.  
  125.     if (!fLocalAddr)
  126.     {
  127.         for (; ad_sp<=IOP480_ADDR_EPROM && !IOP480_IsAddrSpaceActive(hPlx, ad_sp); ad_sp++)
  128.         if (ad_sp>IOP480_ADDR_EPROM)
  129.         {
  130.             printf ("No active memory spaces on board!\n");
  131.             return;
  132.         }
  133.     }
  134.  
  135.     do
  136.     {
  137.         printf ("Access the board's %s ranges\n",pcMemoryType);
  138.         printf ("-------------------------------------------\n");
  139.         printf ("(Access to invalid %s may hang the computer!)\n", pcMemoryType);
  140.         if (!fLocalAddr)
  141.             printf ("1. Change active memory space: %s\n",PLX_GetAddrRangeName(ad_sp));
  142.         printf ("2. Toggle active mode: %s\n", 
  143.             ad_mode==IOP480_MODE_BYTE ? "BYTE (8 bit)" :
  144.             ad_mode==IOP480_MODE_WORD ? "WORD (16 bit)" : "DWORD (32 bit)");
  145.         printf ("3. Read from board\n");
  146.         printf ("4. Write to board\n");
  147.         printf ("99. Back to main menu\n");
  148.         printf ("\n");
  149.         printf ("Enter option: ");
  150.         cmd = 0;
  151.         fgets(line, sizeof(line), stdin);
  152.         sscanf (line, "%d",&cmd);
  153.         switch (cmd)
  154.         {
  155.         case 1:
  156.             if (!fLocalAddr)
  157.             {
  158.                 printf ("Choose memory space:\n");
  159.                 printf ("--------------------\n");
  160.                 for (i=IOP480_ADDR_SPACE0; i<=IOP480_ADDR_EPROM; i++)
  161.                 {
  162.                     printf ("%d. %s", i, PLX_GetAddrRangeName(i));
  163.                     if (IOP480_IsAddrSpaceActive(hPlx, i)) printf ("\n");
  164.                     else printf (" - space not active\n");
  165.                 }
  166.                 printf ("Enter option: ");
  167.                 cmd2 = 99;
  168.                 fgets(line, sizeof(line), stdin);
  169.                 sscanf (line, "%d",&cmd2);
  170.                 if (cmd2>=IOP480_ADDR_SPACE0 && cmd2<=IOP480_ADDR_EPROM)
  171.                 {
  172.                     int new_ad_sp = cmd2;
  173.                     if (IOP480_IsAddrSpaceActive(hPlx, new_ad_sp)) ad_sp = new_ad_sp;
  174.                     else printf ("Chosen space not active!\n");
  175.                 }
  176.             }
  177.             break;
  178.         case 2:
  179.             ad_mode = (ad_mode + 1) % 3;
  180.             break;
  181.         case 3:
  182.             printf ("Enter %s to read from: ", pcMemoryType);
  183.             fgets(line, sizeof(line), stdin);
  184.             sscanf (line, "%x", &addr);
  185.             switch (ad_mode)
  186.             {
  187.             case IOP480_MODE_BYTE:
  188.                 if (fLocalAddr) data = IOP480_ReadByteLocal(hPlx, addr);
  189.                 else data = IOP480_ReadByte(hPlx, ad_sp, addr);
  190.                 break;
  191.             case IOP480_MODE_WORD:
  192.                 if (fLocalAddr) data = IOP480_ReadWordLocal(hPlx, addr);
  193.                 else data = IOP480_ReadWord(hPlx, ad_sp, addr);
  194.                 break;
  195.             case IOP480_MODE_DWORD:
  196.                 if (fLocalAddr) data = IOP480_ReadDWordLocal(hPlx, addr);
  197.                 else data = IOP480_ReadDWord(hPlx, ad_sp, addr);
  198.                 break;
  199.             }
  200.             printf ("Value read: %x\n", data);
  201.             break;
  202.         case 4:
  203.             printf ("Enter %s to write to: ", pcMemoryType);
  204.             fgets(line, sizeof(line), stdin);
  205.             sscanf (line, "%x", &addr);
  206.             printf ("Enter data to write %s: ",
  207.                 ad_mode==IOP480_MODE_BYTE ? "BYTE (8 bit)" :
  208.                 ad_mode==IOP480_MODE_WORD ? "WORD (16 bit)" : "DWORD (32 bit)");
  209.             fgets(line, sizeof(line), stdin);
  210.             sscanf (line, "%x",&data);
  211.             switch (ad_mode)
  212.             {
  213.             case IOP480_MODE_BYTE:
  214.                 if (fLocalAddr) IOP480_WriteByteLocal(hPlx, addr, (BYTE) data);
  215.                 else IOP480_WriteByte(hPlx, ad_sp, addr, (BYTE) data);
  216.                 break;
  217.             case IOP480_MODE_WORD:
  218.                 if (fLocalAddr) IOP480_WriteWordLocal(hPlx, addr, (WORD) data);
  219.                 else IOP480_WriteWord(hPlx, ad_sp, addr, (WORD) data);
  220.                 break;
  221.             case IOP480_MODE_DWORD:
  222.                 if (fLocalAddr) IOP480_WriteDWordLocal(hPlx, addr, data);
  223.                 else IOP480_WriteDWord(hPlx, ad_sp, addr, data);
  224.                 break;
  225.             }
  226.             break;
  227.         }
  228.     } while (cmd!=99);
  229. }
  230.  
  231. void WINAPI PLX_IntHandlerRoutine(IOP480_HANDLE hPlx, IOP480_INT_RESULT *intResult)
  232. {
  233.     printf ("Got interrupt number %d\n", intResult->dwCounter);
  234. }
  235.  
  236. void PLX_EnableDisableInterrupts(IOP480_HANDLE hPlx)
  237. {
  238.     int cmd;
  239.  
  240.     printf ("WARNING!!!\n");
  241.     printf ("----------\n");
  242.     printf ("Your hardware has level sensitive interrupts.\n");
  243.     printf ("You must modify the source code of IOP480_IntEnable(), in the file IOP480_lib.c,\n");
  244.     printf ("to acknowledge the interrupt before enabling interrupts.\n");
  245.     printf ("Without this modification, your PC will HANG upon interrupt!\n");
  246.  
  247.     do
  248.     {
  249.         printf ("Enable / Disable interrupts\n");
  250.         printf ("---------------------------\n");
  251.         printf ("1. %s interrupts\n", IOP480_IntIsEnabled(hPlx) ? "Disable" : "Enable");
  252.         printf ("99. Back to main menu\n");
  253.         printf ("\n");
  254.         printf ("Enter option: ");
  255.         cmd = 0;
  256.         fgets(line, sizeof(line), stdin);
  257.         sscanf (line, "%d",&cmd);
  258.         switch (cmd)
  259.         {
  260.         case 1:
  261.             if (IOP480_IntIsEnabled(hPlx))
  262.             {
  263.                 printf ("Disabling interrupt Int\n");
  264.                 IOP480_IntDisable(hPlx);
  265.             }
  266.             else
  267.             {
  268.                 printf ("Enabling interrupts\n");
  269.                 if (!IOP480_IntEnable(hPlx, PLX_IntHandlerRoutine))
  270.                     printf ("failed enabling interrupts\n");
  271.             }
  272.             break;
  273.         }
  274.     } while (cmd!=99);
  275. }
  276.  
  277. void PLX_EEPROMAccess(IOP480_HANDLE hPlx)
  278. {
  279.     int cmd;
  280.     DWORD addr;
  281.     DWORD dwData;
  282.  
  283.     do
  284.     {
  285.         printf ("Access the board's serial EERPOM\n");
  286.         printf ("--------------------------------\n");
  287.         if (!IOP480_EEPROMValid(hPlx))
  288.             printf ("Note: PLX EEPROM valid BIT is 0\n");
  289.         printf (" Note: reading/writing to odd addresses is meaningless,\n");
  290.         printf ("       as data is stored in words.\n");
  291.         printf ("1. Display EEPROM content\n");
  292.         printf ("2. Read dword from serial EEPROM on the board\n");
  293.         printf ("3. Write dword to the serial EEPROM on the board\n");
  294.         printf ("99. Back to main menu\n");
  295.         printf ("\n");
  296.         printf ("Enter option: ");
  297.         cmd = 0;
  298.         fgets(line, sizeof(line), stdin);
  299.         sscanf (line, "%d",&cmd);
  300.         switch (cmd)
  301.         {
  302.         case 1:
  303.             for (addr=0; addr<0xff; addr += 4)
  304.             {
  305.                 if (!(addr % 0x10))
  306.                 printf("\n %02x: ", addr);
  307.                 if (!IOP480_EEPROMReadDWord(hPlx, addr, &dwData))
  308.                 {
  309.                     printf("\nError occured reading serial EEPROM - %s\n", IOP480_ErrorString);
  310.                     break;
  311.                 }
  312.                 printf("%08x  ", dwData);
  313.             }
  314.             printf ("\n");
  315.             break;
  316.         case 2:
  317.             printf ("Enter addr to read from (0-ff): ");
  318.             fgets(line, sizeof(line), stdin);
  319.             sscanf (line, "%x", &addr);
  320.             if (IOP480_EEPROMReadDWord(hPlx, addr, &dwData))
  321.                 printf ("Value read: %x\n", dwData);
  322.              else
  323.                 printf("Error occured reading serial EEPROM - %s\n", IOP480_ErrorString);
  324.             break;
  325.  
  326.         case 3:
  327.             printf ("Enter addr to write to (0-ff): ");
  328.             fgets(line, sizeof(line), stdin);
  329.             sscanf (line, "%x", &addr);
  330.             printf ("Enter data to write: ");
  331.             fgets(line, sizeof(line), stdin);
  332.             sscanf (line, "%x",&dwData);
  333.             if (!IOP480_EEPROMWriteDWord(hPlx, addr, dwData))
  334.                 printf("Error occured reading serial EEPROM - %s\n", IOP480_ErrorString);
  335.             break;
  336.  
  337.         default:
  338.             break;
  339.         }
  340.     } while (cmd!=99);
  341. }
  342.  
  343. IOP480_HANDLE PLX_LocateAndOpenBoard(DWORD dwVendorID, DWORD dwDeviceID, BOOL fUseInt)
  344. {
  345.     DWORD cards, my_card;
  346.     IOP480_HANDLE hPlx = NULL;
  347.  
  348.     if (dwVendorID==0)
  349.     {
  350.         printf ("Enter VendorID: ");
  351.         fgets(line, sizeof(line), stdin);
  352.         sscanf (line, "%x",&dwVendorID);
  353.         if (dwVendorID==0) return NULL;
  354.  
  355.         printf ("Enter DeviceID: ");
  356.         fgets(line, sizeof(line), stdin);
  357.         sscanf (line, "%x",&dwDeviceID);
  358.     }
  359.     cards = IOP480_CountCards (dwVendorID, dwDeviceID);
  360.     if (cards==0) 
  361.     {
  362.         printf("%s", IOP480_ErrorString);
  363.         return NULL;
  364.     }
  365.     else if (cards==1) my_card = 1;
  366.     else
  367.     {
  368.         DWORD i;
  369.  
  370.         printf("Found %d matching PCI cards\n", cards);
  371.         printf("Select card (1-%d): ", cards);
  372.         i = 0;
  373.         fgets(line, sizeof(line), stdin);
  374.         sscanf (line, "%d",&i);
  375.         if (i>=1 && i <=cards) my_card = i;
  376.         else 
  377.         {
  378.             printf ("Choice out of range\n");
  379.             return NULL;
  380.         }
  381.     }
  382.     if (IOP480_Open (&hPlx, dwVendorID, dwDeviceID, my_card - 1, fUseInt ? IOP480_OPEN_USE_INT : 0))
  383.         printf ("PLX IOP 480 PCI card found!\n");
  384.     else printf ("%s", IOP480_ErrorString);
  385.     return hPlx;
  386. }
  387.  
  388. int main(int argc, char *argv[])
  389. {
  390.     int cmd;
  391.     IOP480_HANDLE hPlx = NULL;
  392.     HANDLE hWD;
  393.     BOOL fUseInt = FALSE; // by default - do not install interrupts
  394.     BOOL fOpenedWithInt = fUseInt;
  395.  
  396.     printf ("PLX IOP 480 diagnostic utility.\n");
  397.     printf ("Application accesses hardware using " WD_PROD_NAME ".\n");
  398.  
  399.     // make sure WinDriver is loaded
  400.     if (!PCI_Get_WD_handle(&hWD)) return 0;
  401.     WD_Close (hWD);
  402.  
  403.     hPlx = PLX_LocateAndOpenBoard(0x10b5, 0x0480, fUseInt);
  404.  
  405.     do
  406.     {
  407.         printf ("\n");
  408.         printf ("PLX IOP 480 main menu\n");
  409.         printf ("-------------------\n");
  410.         printf ("1. Scan PCI bus\n");
  411.         printf ("2. Set opening board %s interrupts\n", fUseInt ? "without" : "with");
  412.         printf ("3. Locate/Choose PLX IOP 480 board (%s interrupts)\n", fUseInt ? "with" : "without");
  413.         if (hPlx)
  414.         {
  415.             printf ("4. PCI configuration registers\n");
  416.             printf ("5. PLX IOP 480 local registers\n");
  417.             printf ("6. Access address spaces on the board\n");
  418.             printf ("7. Access local address ranges on the board\n");
  419.             if (fOpenedWithInt)
  420.                 printf ("8. Enable / Disable interrupts\n");
  421.             printf ("9. Access serial EEPROM on the board\n");
  422.         }
  423.         printf ("99. Exit\n");
  424.         printf ("Enter option: ");
  425.         cmd = 0;
  426.         fgets(line, sizeof(line), stdin);
  427.         sscanf (line, "%d",&cmd);
  428.         switch (cmd)
  429.         {
  430.         case 1: // Scan PCI bus
  431.             PCI_Print_all_cards_info();
  432.             break;
  433.         case 2: // Set open board with / without interrupts
  434.             fUseInt = !fUseInt;
  435.             break;
  436.         case 3: // Locate PLX IOP 480 board
  437.             if (hPlx) IOP480_Close(hPlx);
  438.             hPlx = PLX_LocateAndOpenBoard(0, 0, fUseInt);
  439.             if (!hPlx) printf ("PLX card open failed!\n");
  440.             fOpenedWithInt = fUseInt;
  441.             break;
  442.         case 4: // PCI configuration registers
  443.             if (hPlx) 
  444.             {
  445.                 WD_PCI_SLOT pciSlot;
  446.                 IOP480_GetPciSlot(hPlx, &pciSlot);
  447.                 PCI_EditConfigReg(pciSlot);
  448.             }
  449.             break;
  450.         case 5: // PLX IOP 480 local registers
  451.             if (hPlx) PLX_EditReg(hPlx);
  452.             break;
  453.         case 6: // Access address spaces on the board
  454.             if (hPlx) PLX_BoardAccess(hPlx, FALSE);
  455.             break;
  456.         case 7: // Access local address ranges on the board
  457.             if (hPlx) PLX_BoardAccess(hPlx, TRUE);
  458.             break;
  459.         case 8: // Enable / Disable interrupts
  460.             if (hPlx && fOpenedWithInt) PLX_EnableDisableInterrupts(hPlx);
  461.             break;
  462.         case 9: // Access serial EEPROM on the board
  463.             if (hPlx) PLX_EEPROMAccess(hPlx);
  464.             break;
  465.         }
  466.     } while (cmd!=99);
  467.  
  468.     if (hPlx) IOP480_Close(hPlx);
  469.  
  470.     return 0;
  471. }
  472.